let answers = {};
let timu_id = 0;
let select1 = 1;
let frame_left = 0;

function loadQuestions() {
    var timu = 0;
    var questionIds = [];
    for (var i = 0; i < data1.length; i++) {
        questionIds[i] = data1[i].id;
        var div = document.createElement("div");
        div.className = "entrance-bottom-frame-line";
        document.querySelector(".entrance-bottom-frame").appendChild(div);

        let isSingle = eval(data1[i].single);
        let content = data1[i].content;
        if (isSingle) {
            content = content + "(单选题)"
        } else {
            content = content + "(多选题)"
        }
        var div2 = document.createElement("div");
        div2.className = "entrance-bottom-frame-line-title";
        div2.innerHTML = content;
        document.querySelectorAll(".entrance-bottom-frame-line")[i].appendChild(div2);


        var divli1 = document.createElement("div");
        divli1.innerHTML = parseInt(i) + 1;
        for (var j = 0; j < data1[i].answers.length; j++) {

            var div3 = document.createElement("div");
            var selectValue = data1[i].id + ":" + data1[i].answers[j].no + ":" + data1[i].single;
            div3.value = selectValue;
            div3.addEventListener('click', selectAnswer);
            div3.className = "entrance-bottom-frame-line-button";
            var div3_id = document.createElement("div");
            div3_id.className = "entrance-bottom-frame-line-button-id";
            if (j == 0) {
                div3_id.innerHTML = "A";
            } else if (j == 1) {
                div3_id.innerHTML = "B";
            } else if (j == 2) {
                div3_id.innerHTML = "C";
            } else if (j == 3) {
                div3_id.innerHTML = "D";
            } else if (j == 4) {
                div3_id.innerHTML = "E";
            }
            var div4 = document.createElement("div");
            div4.className = "entrance-bottom-frame-line-button-frame";
            div4.innerHTML = data1[i].answers[j].title;
            div3.appendChild(div3_id)
            div3.appendChild(div4);
            document.querySelectorAll(".entrance-bottom-frame-line")[i].appendChild(div3);
        }
        timu++
    }
    mintime = 1;
    var dact = document.querySelector(".entrance-bottom-frame-line")
    var active = "active"
    var none = "none"
    addClass(dact, active)

    document.querySelector(".entrance-bottom-frame").style.marginLeft = frame_left + "%"
    document.querySelector(".topic-frameli").innerHTML = "第 " + "<div>" + select1 + "</div>" + "/" + timu + " 题"


    document.getElementById("next").onclick = function () {
        if (timu_id < document.querySelectorAll(".entrance-bottom-frame-line").length - 1) {
            frame_left += -100
            document.querySelector(".entrance-bottom-frame").style.marginLeft = frame_left + "%"

            timu_id++;
            select1++;
            document.querySelector(".topic-frameli").innerHTML = "第 " + "<div>" + select1 + "</div>" + "/" + timu + " 题"
            addClass(document.querySelectorAll(".entrance-bottom-frame-line")[timu_id], active)
            removeClass(document.querySelectorAll(".entrance-bottom-frame-line")[timu_id - 1], active)
        } else {
            alert("最后一道题啦");
        }
    }

    /*for (var i = 0; i < document.querySelectorAll(".entrance-bottom-frame-line-button").length; i++) {
        document.querySelectorAll(".entrance-bottom-frame-line-button")[i].onclick = function () {




            if (timu_id < document.querySelectorAll(".entrance-bottom-frame-line").length - 1) {
                frame_left += -100
                document.querySelector(".entrance-bottom-frame").style.marginLeft = frame_left + "%"

                timu_id++;
                select1++;
                document.querySelector(".topic-frameli").innerHTML = "第 " + "<div>" + select1 + "</div>" + "/" + timu + " 题"
                addClass(document.querySelectorAll(".entrance-bottom-frame-line")[timu_id], active)
                removeClass(document.querySelectorAll(".entrance-bottom-frame-line")[timu_id - 1], active)
            } else {
                // alert("最后一道题啦")
            }
        }
    }*/
}

function addClass(obj, cls) {
    var obj_class = obj.className,//获取 class 内容.
        blank = (obj_class != '') ? ' ' : '';//判断获取到的 class 是否为空, 如果不为空在前面加个'空格'.
    added = obj_class + blank + cls;//组合原来的 class 和需要添加的 class.
    obj.className = added;//替换原来的 class.
}

function removeClass(obj, cls) {
    var obj_class = ' ' + obj.className + ' ';//获取 class 内容, 并在首尾各加一个空格. ex) 'abc    bcd' -> ' abc    bcd '
    obj_class = obj_class.replace(/(\s+)/gi, ' '),//将多余的空字符替换成一个空格. ex) ' abc    bcd ' -> ' abc bcd '
        removed = obj_class.replace(' ' + cls + ' ', ' ');//在原来的 class 替换掉首尾加了空格的 class. ex) ' abc bcd ' -> 'bcd '
    removed = removed.replace(/(^\s+)|(\s+$)/g, '');//去掉首尾空格. ex) 'bcd ' -> 'bcd'
    obj.className = removed;//替换原来的 class.
}


function hasClass(obj, cls) {
    var obj_class = obj.className,//获取 class 内容.
        obj_class_lst = obj_class.split(/\s+/);//通过split空字符将cls转换成数组.
    x = 0;
    for (x in obj_class_lst) {
        if (obj_class_lst[x] == cls) {//循环数组, 判断是否包含cls
            return true;
        }
    }
    return false;
}

function selectAnswer() {
    let kv = this.value.split(":");
    let key = kv[0];
    let value = kv[1];
    let isSingle = eval(kv[2]);
    console.log(this.value);
    if (isSingle) {
        single(this, key, value);
    } else {
        multiple(this, key, value);
    }
    console.log(answers);
}

function single(object, key, value) {
    let values = [value];
    answers[key] = values;
    let brother = object.parentNode.children;
    for (let i = 0; i < brother.length; i++) {
        if (brother[i].className == "entrance-bottom-frame-line-button-onclick") {
            brother[i].className = "entrance-bottom-frame-line-button";
        }
    }
    object.className = "entrance-bottom-frame-line-button-onclick";
}

function multiple(object, key, value) {
    let values = answers[key];
    if (typeof values == "undefined") {
        //无选项
        values = [value];
    } else {
        let index = values.indexOf(value);
        if (index > -1) {
            //删除要取消的
            values.splice(index, 1);
            // values.remove(kv[1]);
        } else {
            //添加要选择的
            values.push(value);
        }

    }
    answers[key] = values;

    if (object.className == "entrance-bottom-frame-line-button") {
        object.className = "entrance-bottom-frame-line-button-onclick";
    } else {
        object.className = "entrance-bottom-frame-line-button";
    }
}


